package com.hyj.springcloud.lb;

import org.springframework.cloud.client.ServiceInstance;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * my 学习irule的RoudRobinRule轮询源码,仿写loadBalance
 * 不加@Component还真扫描不到.(boot不扎实.)
 */
@Component
public class LoadBalanceImpl implements LoadBalance {
    private AtomicInteger atomicInteger = new AtomicInteger(0);

    public final int getAndIncr() {
        int current;
        int next;
        do {
            current = this.atomicInteger.get();
            //Integer.Max_Value=21
            next = current > 2147483647 ? 0 : current + 1;
        } while (!this.atomicInteger.compareAndSet(current,next));
        return next;
    }

    @Override
    public ServiceInstance instances(List<ServiceInstance> serviceInstanceList) {
        return null;
    }
}
